package com.etoak.controller;

import com.etoak.common.utils.DateUtils;
import com.etoak.common.utils.uuid.IdUtils;
import com.etoak.entity.CheckList;
import com.etoak.entity.CheckListApprove;
import com.etoak.service.ICheckListApproveService;
import com.etoak.service.IChecklistService;
import com.etoak.util.StringUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Checklist库审核Controller
 *
 * @author zhixuebin
 * @date 2022-05-12
 */
@RestController
@RequestMapping("/goertek/checklist/approve")
public class CheckListApproveController extends BaseController {

    @Autowired
    ICheckListApproveService checkListApproveService;

    @Autowired
    IChecklistService checklistService;

    /**
     * 知识审核(主管审核)
     * @param approve
     * @return
     */
    @Transactional
    @PostMapping("/check")
    public AjaxResult check(@RequestBody CheckListApprove approve){
        //查询主表对应记录
        CheckList list = checklistService.getChecklistItemById(approve.getKnowledgeId());
        //非空判断
        /*if(StringUtils.isNotNull(list)){
            //判断状态是否正确
            if(!list.getStatus().equals("2")){
                return AjaxResult.error("当前记录不可审核!");
            }
        }*/
        //设置主键
        String id = IdUtils.simpleUUID();
        approve.setId(id);
        String[] approveTypes = approve.getApproveTypes();
        List result = new ArrayList();
        result.addAll(Arrays.asList(approveTypes));
        approve.setApproveType(result.toString().replace("[","").replace("]",""));
        approve.setApproveTime(DateUtils.getNowDate());
        approve.setApproveNode("0");
        int r = 0;
        /*根据审核意见更新主表记录状态*/
        if(approve.getApproveStatus().equals("0")){
            //如果审核通过,则修改主表记录status为3
            list.setStatus(3);
            r = checklistService.updateChecklist(list);
        }
        int i= checkListApproveService.check(approve);
        if(i > 0){
            return AjaxResult.success("审核成功");
        }else {
            return AjaxResult.error("审核失败");
        }
    }

    /**
     * 专家会签
     * @param approve
     * @return
     */
    @PostMapping("/expertCheck")
    public AjaxResult expertCheck(@RequestBody CheckListApprove approve){
        CheckList result = checklistService.getChecklistItemById(approve.getKnowledgeId());
        return AjaxResult.success();
    }

    /**
     * 获取待审核列表
     * @return
     */
    @GetMapping("/getItemToApprove")
    public AjaxResult getItemToApprove(){
        List<CheckList> unApprovedList = checklistService.getUnApprovedList();
        if(unApprovedList != null || unApprovedList.size() != 0){
            return AjaxResult.success(unApprovedList);
        }else{
            return AjaxResult.success("暂无待审项");
        }
    }



}
